/* Andreas Baum
 * ClientThread.java
 * Handles the interaction of the IM client with the user and the IM server
 * CS 78 - Networks - Programming Assignment 1
 */

package ChatClientApplet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class AppletThread extends Thread {
	private Socket socket = null;	
	private PrintWriter out = null;
	private BufferedReader in = null;
	private boolean disconnect = false;
	private ChatClientPanel chatClientPanel;

	public AppletThread(Socket socket, String username, ChatClientPanel panel) {
		super("AppletThread");
		this.socket = socket;
		chatClientPanel = panel;

		try {
			// establish in and out streams for communication with server
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		out.println("-WHO");
		
		// start listening to the socket
		this.start();
	}

	public void run() {

		String line = null;
		try {
			// listen to the in stream. If a non-null line is 
			// received analyze it and act appropriately
			while ( ( ( line = in.readLine() ) != null ) 
					&& !socket.isClosed(  ) ) {

				// read the first word of the line 
				// which corresponds to a command
				String command = (line.split(" ")[0]).toUpperCase();

				// if command is PRINT, information, don't preface message 
				if (command.equals("PRINT")) {
					System.out.println(line.substring(6));
					chatClientPanel.appendOutput(line.substring(6));
					continue;
				}
				
				// if command is PM, information, display it in the PM window  
				if (command.equals("PM")) {
					System.out.println(line.substring(3));
					chatClientPanel.appendPM(line.substring(3));
					continue;
				}

				// if command is ERR, print the Error message.
				// NOTE: Error messages are marked separately from other
				// messages. This is not a current requirement, but 
				// the distinction may become useful with added functionality.
				if (command.equals("ERR")) {
					System.out.println(line.substring(4));
					chatClientPanel.appendOutput(line.substring(4));
					continue;
				}
				
				// if command is MEM, update the member list.
				if (command.equals("MEM")) {
					updateMembers(line.substring(4));
					continue;
				}

				// if command is EXT, disconnect.
				if (command.equals("EXT")) {
					System.out.println("Disconnected.");
					chatClientPanel.appendOutput("Disconnected.");
					disconnect = true;
					out.close();
					in.close();
					socket.close();
					break;
				}				
			}
		} catch (IOException e) {
			// In case of exception:
			e.printStackTrace();
		} catch (Exception e) {
			// In case of an exception:
			e.printStackTrace();
		}

		// If the server has disconnected:
		// (the variable disconnect is only true if 
		// the user requested to disconnect. Otherwise
		// assume that the server disconnected)
		if (!disconnect) {
			System.out.println("Server disconnected.");
			chatClientPanel.appendOutput("Server disconnected.");
			System.exit(1);
		}
	}
	
	public void updateMembers(String string) {
		System.out.println("Got a request to update: " + string);
		chatClientPanel.updateMembers(string);
	}

	public void send(String string) {
		out.println(string);
	}
}